package com.wonders.module.demo.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wonders.common.constant.TipConstant;
import com.wonders.framework.entity.vo.Page;
import com.wonders.framework.exception.BusinessException;
import com.wonders.framework.util.bean.BeanUtil;
import com.wonders.module.demo.dao.DemoDao;
import com.wonders.module.demo.entity.bo.Demo;
import com.wonders.module.demo.entity.vo.DemoVo;
import com.wonders.module.demo.service.DemoService;

@Service
public class DemoServiceImpl implements DemoService {

	@Autowired
	private DemoDao demoDao;

	public Page query(DemoVo demoVo, Page page) {
		page = demoDao.query(demoVo, page);
		return BeanUtil.copyPage(page, DemoVo.class);
	}

	public void save(DemoVo demoVo) {
		Demo demo = BeanUtil.copyProperties(demoVo, Demo.class);
		demo.setTime(new Date());
		demoDao.save(demo);
	}

	public DemoVo load(Long id) {
		Demo demo = demoDao.get(Demo.class, id);
		if (demo == null) {
			throw new BusinessException(TipConstant.DATA.NOT_FOUND);
		}
		return BeanUtil.copyProperties(demo, DemoVo.class);
	}

	public void update(DemoVo demoVo) {
		Demo demo = demoDao.get(Demo.class, demoVo.getId());
		if (demo == null) {
			throw new BusinessException(TipConstant.DATA.NOT_FOUND);
		}
		BeanUtil.copyProperties(demoVo, demo);
		demoDao.update(demo);
	}

	public void remove(List<Long> keys) {
		demoDao.remove(keys);
	}

}
